package com.sunjob.mrotech.server.Impl;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.sunjob.common.domain.loginServer.SysUser;
import com.sunjob.common.domain.tech.RepairProcess2;
import com.sunjob.common.domain.tech.RepairProcess2DO;
import com.sunjob.common.domain.tech.TransShape2;
import com.sunjob.mrotech.mapper.RepairProcess2Mapper;
import com.sunjob.mrotech.server.RepairProcess2Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @author : xiaozhoutongxue
 * @date : 2022/8/9 15:13
 */
@Service
public class RepairProcess2ServerImpl implements RepairProcess2Server {
    @Autowired
    RepairProcess2Mapper repairProcess2Mapper;

    public String getUserName() {
        SaSession tokenSession = StpUtil.getTokenSession();
        SysUser sysUser = (SysUser) tokenSession.get("userInfo");
        return sysUser.getUserName();
    }

    @Override
    public List<TransShape2> getRepairProcess2ByLimit3() {
        return repairProcess2Mapper.getRepairProcess2ByLimit3();
    }

    @Override
    public RepairProcess2 getRepairProcess2ByShape2Id(Long shape2Id) {
        return repairProcess2Mapper.getRepairProcess2ByShape2Id(shape2Id);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public String insertRepairProcess2(RepairProcess2 repairProcess2) {
        try {
            if(repairProcess2.getSteps().size()==0){
                return "至少有一个步骤才行哦";
            }
            for (int i = 0; i < repairProcess2.getSteps().size(); i++) {
                RepairProcess2DO repairProcess2DO = new RepairProcess2DO();
                repairProcess2DO.setRepairProcess2Id(repairProcess2.getRepairProcess2Id());
                repairProcess2DO.setRepairProcess2Name(repairProcess2.getRepairProcess2Name());
                repairProcess2DO.setStep(repairProcess2.getSteps().get(i).getOrder());
                repairProcess2DO.setText(repairProcess2.getSteps().get(i).getText());
                repairProcess2DO.setPicId(repairProcess2.getSteps().get(i).getPicId());
                repairProcess2DO.setVersion(1L);
                repairProcess2DO.setCreateBy(getUserName());
                repairProcess2DO.setCreateTime(LocalDateTime.now());
                repairProcess2Mapper.insertRepairProcess2ById(repairProcess2DO);
            }
        } catch (Exception e) {
            return "数据增加失败";
        }
        return "true";
    }

    @Override
    public Boolean bindRepairProcessByShape2Id(Long shape2Id, Long repairProcess2Id) {
        int i = repairProcess2Mapper.bindRepairProcessByShape2Id(shape2Id, repairProcess2Id, getUserName(), LocalDateTime.now());
        return i > 0 ? true : false;
    }

    @Override
    public String deleteRepairProcess2ById(Long repairProcess2Id) {
        RepairProcess2 repairProcess2 = repairProcess2Mapper.getRepairProcess2ById(repairProcess2Id);
        if (repairProcess2 == null) {
            return "未找到该修程工艺";
        }
        Long version = repairProcess2.getVersion();
        int i = repairProcess2Mapper.deleteRepairProcess2ById(repairProcess2Id, version, getUserName(), LocalDateTime.now());
        return i > 0 ? "true" : "删除失败";
    }

    @Override
    public Boolean uptateRepairProcess2Small(RepairProcess2DO repairProcess2DO1) {
        RepairProcess2DO repairProcess2DO =new RepairProcess2DO();
        repairProcess2DO.setRepairProcess2Id(repairProcess2DO1.getRepairProcess2Id());
        repairProcess2DO.setStep(repairProcess2DO1.getStep());
        repairProcess2DO.setVersion(repairProcess2DO1.getVersion());
        repairProcess2DO.setText(repairProcess2DO1.getText());
        repairProcess2DO.setPicId(repairProcess2DO1.getPicId());
        repairProcess2DO.setUpdateBy(getUserName());
        repairProcess2DO.setStatus(0);
        repairProcess2DO.setUpdateTime(LocalDateTime.now());
        int i = repairProcess2Mapper.uptateRepairProcess2Small(repairProcess2DO);
        return i > 0 ? true : false;
    }

    @Override
    @Transactional
    public Boolean uptateRepairProcess2Big(RepairProcess2 newRepairProcess2) {
        //得到原来数据库的信息并且更新状态
        Long repairProcess2Id = newRepairProcess2.getRepairProcess2Id();
        RepairProcess2 oldRepairProcess2 = repairProcess2Mapper.getRepairProcess2ById(repairProcess2Id);
        for (int i = 0; i < oldRepairProcess2.getSteps().size(); i++) {
            RepairProcess2DO repairProcess2DO = new RepairProcess2DO();
            repairProcess2DO.setRepairProcess2Id(oldRepairProcess2.getRepairProcess2Id());
            repairProcess2DO.setStep(i+1);
            repairProcess2DO.setVersion(oldRepairProcess2.getVersion());
            repairProcess2DO.setText(oldRepairProcess2.getSteps().get(i).getText());
            repairProcess2DO.setPicId(oldRepairProcess2.getSteps().get(i).getPicId());
            repairProcess2DO.setStatus(1);
            repairProcess2DO.setUpdateBy(getUserName());
            repairProcess2DO.setUpdateTime(LocalDateTime.now());
            repairProcess2Mapper.uptateRepairProcess2Small(repairProcess2DO);
        }
        //增加新的版本号的数据
        Long newVersion = oldRepairProcess2.getVersion() + 1;
        for (int i = 0; i < newRepairProcess2.getSteps().size(); i++) {
            RepairProcess2DO repairProcess2DO = new RepairProcess2DO();
            repairProcess2DO.setRepairProcess2Id(newRepairProcess2.getRepairProcess2Id());
            repairProcess2DO.setRepairProcess2Name(oldRepairProcess2.getRepairProcess2Name());
            repairProcess2DO.setStep(newRepairProcess2.getSteps().get(i).getOrder());
            repairProcess2DO.setText(newRepairProcess2.getSteps().get(i).getText());
            repairProcess2DO.setPicId(newRepairProcess2.getSteps().get(i).getPicId());
            repairProcess2DO.setVersion(newVersion);
            repairProcess2DO.setCreateBy(oldRepairProcess2.getCreateBy());
            repairProcess2DO.setCreateTime(oldRepairProcess2.getCreateTime());
            repairProcess2DO.setUpdateBy(getUserName());
            repairProcess2DO.setUpdateTime(LocalDateTime.now());
            repairProcess2DO.setStatus(0);
            repairProcess2Mapper.insertRepairProcess2ById(repairProcess2DO);
        }
        return true;
    }

}
